package com.xcyy.zjdxmjk.zjdxmjk.controller;

import com.xcyy.zjdxmjk.zjdxmjk.VO.Gzzz2VO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.Gzzz3VO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.GzzzVO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.ResultVO;
import com.xcyy.zjdxmjk.zjdxmjk.common.ResultVOUtil;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Country;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Town;
import com.xcyy.zjdxmjk.zjdxmjk.service.CountryService;
import com.xcyy.zjdxmjk.zjdxmjk.service.TownService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GzzzController {
    @Autowired
    private CountryService countryService;
    @Autowired
    private TownService townService;

    @PostMapping("/gzzz")
    private ResultVO gzzz(){
        List<Country> countries=countryService.findAll();
        List<GzzzVO> gzzzVOS=new ArrayList<>();
        GzzzVO gzzzVO;
        for(Country country:countries){
            gzzzVO=new GzzzVO();
            BeanUtils.copyProperties(country,gzzzVO);
            gzzzVO.setZs(country.getJsydCount()+country.getZjdCount());
            gzzzVOS.add(gzzzVO);
        }
        return ResultVOUtil.success(gzzzVOS);
    }

    @PostMapping("/dcjggs")
    private ResultVO dcjggs(){
        List<Country> countries=countryService.findAll();
        List<Gzzz2VO> gzzz2VOS=new ArrayList<>();
        Gzzz2VO gzzz2VO;
        String codeStr;
        for(Country country:countries){
            gzzz2VO=new Gzzz2VO();
            gzzz2VO.setName(country.getName());
            gzzz2VO.setCode(country.getCode());
            codeStr=country.getCode().substring(0,6);
            Object o1=townService.countDcjggsGroupByCode(codeStr);
            gzzz2VO.setWcs(Integer.parseInt(((BigDecimal) ((Object[])o1)[0]).toString()));
            gzzz2VO.setZs(Integer.parseInt(((BigDecimal) ((Object[])o1)[1]).toString()));
            gzzz2VOS.add(gzzz2VO);
        }

        return ResultVOUtil.success(gzzz2VOS);
    }

    @PostMapping("/dcjggsbycode")
    private ResultVO dcjggsByCode(String code){
        String codeStr=code.substring(0,6);
        List<Town> towns=townService.findByCodeStartsWith(codeStr);
        List<Gzzz2VO>  gzzz2VOS=new ArrayList<>();
        Gzzz2VO gzzz2VO;
        for (Town town:towns){
            gzzz2VO=new Gzzz2VO();
            BeanUtils.copyProperties(town,gzzz2VO);
            gzzz2VO.setWcs(town.getDcjggs());
            gzzz2VO.setZs(town.getCunCount());
            gzzz2VOS.add(gzzz2VO);
        }
        return ResultVOUtil.success(gzzz2VOS);
    }
}
